package xx.services.entity.rest;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.RowSet;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.bas.Comment;
import xx.entity.bas.Reply;
import xx.services.Passport;
import xx.services.entity.inter.ICommentInterface;
import xx.services.util.Constants;
import xx.util.Arr;

/**
 * 评论私有服务接口
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/comment")
 * @author dengjinye,2010-12-18 $LastChangedDate$ $Author$
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/comment")
public class CommentRest extends Passport implements ICommentInterface
{

    public CommentRest(@Context
    HttpServletRequest request)
    {
        super(request);
        // TODO Auto-generated constructor stub
    }

    /**
     * 为实体评论{comment_id}增加一条回复。
     * 
     * @POST
     * @Path ("/{comment_id}/reply")
     * @param commentId-@PathParam("comment_id")
     *            评论ID
     * @param title-@FormParam("title")
     *            回复标题
     * @param content-@FormParam("content")
     *            回复内容
     * @return 新增的回复实体
     * @author dengjinye,2010-10-29
     */
    @POST
    @Path("/{comment_id}/reply")
    public String addReply(@PathParam("comment_id")
    long commentId, @FormParam("title")
    String title, @FormParam("content")
    String content)
    {
        try
        {
            Reply reply = new Reply(qr, owner);
            reply.setCommentId(commentId).setTitle(title).setContent(content).setIp(ip);
            reply.insert();
            return ok(reply);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 删除评论{comment_ids}集，同时删除与之关联的回复。
     * 
     * @DELETE
     * @Path ("/{comment_id}")
     * @param commentId-@PathParam("comment_id")
     *            评论ID，多个ID用“_”隔开
     * @return
     * @author dengjinye,2010-10-29
     */
    @DELETE
    @Path("/{comment_id}")
    public String deleteComment(@PathParam("comment_id")
    String commentId)
    {
        try
        {
            long[] commentIdArr = Arr.toLongArray(commentId, "_");
            for (long id : commentIdArr)
            {
                Comment comment = new Comment(qr, owner);
                comment.setCommentId(id);
                comment.delete();
            }
            return ok(true);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 获取对评论{comment_id}的回复列表。
     * 
     * @GET
     * @Path ("/{comment_id}/reply")
     * @param commentId-@PathParam("comment_id")
     *            评论ID
     * @return 回复列表
     * @author dengjinye,2010-10-29
     */
    @GET
    @Path("/{comment_id}/reply")
    public String getReplyList(@PathParam("comment_id")
    long commentId)
    {
        try
        {
            Comment comment = new Comment(qr, owner);
            comment.setCommentId(commentId);
            RowSet rs = comment.setCommentId(commentId).getReplyRowSet(pageSize, pageNo, condition,
                    orderField);

            // 如果是获取第一页的数据，则同时返回总数
            Map<String, Object> map = new HashMap<String, Object>();
            if (isTotal)
            {
                map.put(Constants.TOTAL, comment.getReplyCount(condition));
            }
            return ok(rs, map);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 修改评论{comment_id}的标题内容等。
     * 
     * @PUT
     * @Path ("/{comment_id}")
     * @param commentId-@PathParam("comment_id")
     *            评论ID
     * @param title-@FormParam("title")
     *            标题
     * @param content-@FormParam("content")
     *            内容
     * @return 返回新的评论
     * @author dengjinye,2010-10-29
     */
    @PUT
    @Path("/{comment_id}")
    public String updateComment(@PathParam("comment_id")
    long commentId, @FormParam("title")
    String title, @FormParam("content")
    String content, @FormParam("readed")
    boolean readed)
    {
        try
        {
            Comment comment = new Comment(qr, owner);
            comment.setCommentId(commentId).setTitle(title).setContent(content).setReaded(readed);
            comment.update(Comment.TITLE, Comment.CONTENT);
            return ok(comment);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }
}
